package dk.mirasola.systemtraining.user.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.distributionfiltereditor.DistributionFilterTreeEditor;
import dk.mirasola.systemtraining.user.client.i18n.UserDistributionFilterEditorTexts;
import dk.mirasola.systemtraining.user.shared.transfer.UserDistributionFilterTO;

public class UserDistributionFilterEditor extends Composite {
    interface UserDistributionFilterEditorUiBinder extends UiBinder<Widget,UserDistributionFilterEditor>{}
    private static UserDistributionFilterEditorUiBinder uiBinder = GWT.create(UserDistributionFilterEditorUiBinder.class);
    private static UserDistributionFilterEditorTexts texts = GWT.create(UserDistributionFilterEditorTexts.class);

    @UiField
    TextBox name;
    @UiField
    TextArea description;
    @UiField
    Button saveAndClose;
    @UiField
    Button cancel;
    @UiField(provided = true)
    DistributionFilterTreeEditor distributionFilterTreeEditor;
    @UiField
    HTML nameLabel;
    @UiField
    HTML descriptionLabel;

    private UserDistributionFilterTO value;

    public UserDistributionFilterEditor(UserDistributionFilterTO userDistributionFilter) {
        this.value = userDistributionFilter;
        distributionFilterTreeEditor = new DistributionFilterTreeEditor(userDistributionFilter.getDistributionFilter());
        initWidget(uiBinder.createAndBindUi(this));

        nameLabel.setHTML(texts.nameLabel());
        descriptionLabel.setHTML(texts.descriptionLabel());

        saveAndClose.setHTML(texts.saveAndCloseButton());
        cancel.setHTML(texts.cancelButton());

        initName();
        name.setText(userDistributionFilter.getName());
        description.setText(userDistributionFilter.getDescription());
    }

    private void initName() {
        name.addKeyUpHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent event) {
                if (name.getText().length() == 0) {
                    saveAndClose.setEnabled(false);
                    name.addStyleDependentName("invalid");
                } else {
                    saveAndClose.setEnabled(true);
                    name.removeStyleDependentName("invalid");
                }
            }
        });
    }

    public Button getSaveAndCloseButton() {
        return saveAndClose;
    }

    public Button getCancelButton() {
        return cancel;
    }

    public UserDistributionFilterTO flush() {
        value.setName(name.getText());
        value.setDescription(description.getText());
        value.setDistributionFilter(distributionFilterTreeEditor.flush());
        return value;
    }

    public TextBox getNameTextBox() {
        return name;
    }
}
